package com.dlc.yunguang.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.utils.TimeFormats;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.yunguang.adapter.OrderDetailAdapter;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.base.Constants;
import com.dlc.yunguang.net.bean.WashOrderDetailBean;
import com.dlc.yunguang.net.protocol.OrderProto;
import com.dlc.yunguang.R;
import com.dlc.yunguang.adapter.decoration.RecyclerSpace;

import butterknife.BindView;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by YoungeTao on 2017/7/21
 * QQ 2276559259.
 * gmail youngetao@gmail.com
 */

public class OrderDetailActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    @BindView(R.id.tv_number)
    TextView tv_number;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_company)
    TextView tv_company;
    private int type;
    private String oid;
    private OrderDetailAdapter<WashOrderDetailBean.DataBean.SpecGroupBean> adapter;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTitleBar.leftExit(this);

        type = getIntent().getIntExtra("type",0);
        oid = getIntent().getStringExtra("oid");


        mTitleBar.setTitle(R.string.detail);
        adapter = new OrderDetailAdapter<>();
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, BaseRecyclerAdapter.CommonHolder holder,
                                    int position) {
                startActivity(ProductStatusListActivity.class);
            }
        });

        //adapter.setNewData(details);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerSpace(getResources(), 0, R.dimen.normal_24dp));
        mRecyclerView.setAdapter(adapter);
        // TODO: 2017/7/19 临时代码 ------end

        initData();
    }

    private void initData() {
        if(type == Constants.START_TYPE.WASH_ORDER){
            OrderProto.get().orderWashDetail(String.valueOf(oid),null).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(new Observer<WashOrderDetailBean>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            LogPlus.e(e.getMessage());
                        }

                        @Override
                        public void onNext(WashOrderDetailBean washOrderDetailBean) {
                            if(washOrderDetailBean != null && washOrderDetailBean.getData() != null){
                                adapter.setNewData(washOrderDetailBean.getData().getSpec_group());
                               // tv_order_wash_money.setText("订单洗涤费用:￥"+washOrderDetailBean.getData().getAmount());
                                tv_number.setText("单号:￥"+washOrderDetailBean.getData().getOrderno());
                                tv_time.setText("时间:￥"+washOrderDetailBean.getData().getCtime());
                                tv_company.setText("公司名称:￥"+washOrderDetailBean.getData().getCompany());
                            }
                        }
                    });

        }else{
            OrderProto.get().orderRentDetail(String.valueOf(oid)).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(new Observer<WashOrderDetailBean>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            LogPlus.e(e.getMessage());
                        }

                        @Override
                        public void onNext(WashOrderDetailBean washOrderDetailBean) {
                            if(washOrderDetailBean != null && washOrderDetailBean.getData() != null){
                                adapter.setNewData(washOrderDetailBean.getData().getSpec_group());
                               // tv_order_wash_money.setText("租赁总费用:￥"+washOrderDetailBean.getData().getAmount());
                                tv_number.setText("单号:￥"+washOrderDetailBean.getData().getOrderno());
                                tv_time.setText("时间:￥"+ TimeFormats.formatDateTimeSecend(washOrderDetailBean.getData().getCtime()));
                                tv_company.setText("公司名称:￥"+washOrderDetailBean.getData().getCompany());
                            }
                        }
                    });
        }
    }
}
